<?php

namespace App\Requests\Admin\Goods;

use App\Enums\GoodsTypeStatusEnum;
use App\Models\BrandModel;
use App\Models\GoodsClass;
use App\Models\GoodsType;
use App\Requests\BaseRequest;
use App\Traits\ValidatorTrait;
use Illuminate\Validation\Rule;
use App\Enums\GoodsBusinessEnum;
use App\Enums\GoodsConveyTypeEnum;

/**
 * @package App\Requests\Admin\Goods
 */
class GoodsSaveRequest extends BaseRequest
{
    use ValidatorTrait;


    /**
     * 字段注解
     *
     * @return array
     */
    public function attributes(): array
    {
        return [
            'business_type' => '业务类型',
            'open_sale' => '售卖模式',
            'category_id' => '商品所属品类',
            'brand_id' => '商品品牌',
            'goods_type' => '商品型号',
            'title' => '商品名称',
            'goods_name' => '商品小名称',
            'new_grade' => '商品新旧程度',
//            'specData' => '规格数据',
            'spread_title' => '推广关键字',
            'rent_label' => '租赁标签',
            'convey_type'=>'配送方式',
            'imgurl' => '商品封面图片',
            'carousel_img' => '商品轮播图片',
            'goods_content' => '商品详情',
            'open_jump' => '外跳模式',
            'jump_link' => '外跳链接',
            'discount_type' => '优惠类型',
            'discount_name' => '优惠券名称',
            'member_id'=>'请选择商家',
            'jump_type'=>'外跳方式',
            'goods_sort'=>'排序',
            'position_img'=>'占位图',
            'service_name'=>'服务名称',
            'service_dec'=>'服务描述',
            'service_price'=>'服务价格',
            'open_service'=>'是否开启增值服务',
        ];
    }

    public function rules(): array
    {
        return [
            'business_type'=>['required', 'integer', Rule::in(GoodsBusinessEnum::getKeyList())],
            'open_sale' => ['required', 'integer'],
            'title' => ['required', 'string'],
            'brand_id' => ['sometimes', 'integer', Rule::exists(BrandModel::class, BrandModel::KEY),],
            'category_id' => ['required', 'integer', Rule::exists(GoodsClass::class, GoodsClass::KEY)],
            'rent_label' => ['sometimes'],
            'spread_title' => ['sometimes'],
            'goods_name' => ['sometimes'],
            'service_name' => ['sometimes'],
            'service_dec' => ['sometimes'],
            'service_price' => ['sometimes'],
            'open_service' => ['sometimes'],
            'goods_type' => ['sometimes', 'integer'],
            'goods_sort' => ['sometimes', 'integer'],
//            'specData' => ['required', 'array'],
            'new_grade' => ['required', 'string'],
            'convey_type' => ['required', 'integer', Rule::in(GoodsConveyTypeEnum::getKeyList())],
            'carousel_img' => ['required', 'string'],
            'imgurl' => ['required', 'string'],
            'goods_content' => ['required', 'string'],
            'open_jump' => ['required', 'integer'],
            'member_id' => ['required', 'integer'],
            'jump_link' => ['sometimes'],
            'jump_type' => ['sometimes'],
            'goods_id' => ['sometimes'],
            'discount_type' => ['required', 'integer'],
            'discount_name' => ['sometimes', ],
            'discount_first_amount' => ['sometimes'],
            'discount_first' => ['sometimes'],
            'discount_max_amount' => ['sometimes'],
            'discount_max_sub_amount' => ['sometimes'],
            'position_img' => ['sometimes'],
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);

        foreach ($data as $key=>$ra){
            $data[$key] = $data[$key] ?? '';
        }
        // 设置默认值

        return $data;
    }
}
